package zack.project.domain.activity.service.trial.node;

import zack.project.domain.activity.model.entity.MarketProductEntity;
import zack.project.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import zack.project.domain.activity.model.valobj.SCSkuActivityVO;
import zack.project.domain.activity.model.valobj.SkuVO;
import zack.project.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeoutException;

/**
 * @description 线程案例举例
 * @create 2025-04-03 07:44
 */
@Slf4j
//@Service
public class MarketNode2CompletableFuture extends MarketNode {

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

    /**
     * 异步加载拼团活动配置
     * 通过请求锁单dto构建营销商品实体对象，该实体对象的sc，goodsId -> 查询{sc_sku_activity}获得scSkuActivityVO(渠道商品的活动配置)->
     * 聚到商品活动的活动id去查询group_buy_activity的discountId获取该商品对应活动的折扣配置。
     * 一个渠道下的商品 -> 一个拼团活动 -> 一个折扣配置
     *
     * @param requestParameter
     * @param dynamicContext
     * @throws ExecutionException
     * @throws InterruptedException
     * @throws TimeoutException
     */
    @Override
    protected void multiThread(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws ExecutionException, InterruptedException, TimeoutException {
        // 异步查询活动配置
        CompletableFuture<GroupBuyActivityDiscountVO> groupBuyActivityDiscountVOCompletableFuture =
                CompletableFuture.supplyAsync(() -> {
                    try {
                        Long availableActivityId = requestParameter.getActivityId();
                        if (null == requestParameter.getActivityId()) {
                            // 查询渠道商品活动配置关联配置
                            SCSkuActivityVO scSkuActivityVO = repository.querySCSkuActivityBySCGoodsId(requestParameter.getSource(),
                                    requestParameter.getChannel(),
                                    requestParameter.getGoodsId());
                            if (null == scSkuActivityVO) return null;
                            availableActivityId = scSkuActivityVO.getActivityId();
                        }
                        // 查询活动配置
                        return repository.queryGroupBuyActivityDiscountVO(availableActivityId);
                    } catch (Exception e) {
                        log.error("异步查询活动配置异常", e);
                        return null;
                    }
                }, threadPoolExecutor);

        // 异步查询商品信息 - 在实际生产中，商品有同步库或者调用接口查询。这里暂时使用DB方式查询。
        //查询数据库表{sku}获取sku商品的原价，用于优惠试算
        CompletableFuture<SkuVO> skuVOCompletableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                return repository.querySkuByGoodsId(requestParameter.getGoodsId());
            } catch (Exception e) {
                log.error("异步查询商品信息异常", e);
                return null;
            }
        }, threadPoolExecutor);
        // 等待所有异步任务完成并写入上下文
        CompletableFuture.allOf(groupBuyActivityDiscountVOCompletableFuture, skuVOCompletableFuture)
                .thenRun(() -> {
                    dynamicContext.setGroupBuyActivityDiscountVO(groupBuyActivityDiscountVOCompletableFuture.join());
                    dynamicContext.setSkuVO(skuVOCompletableFuture.join());
                    ;
                }).join();

        log.info("拼团商品查询试算服务-MarketNode userId:{} 异步线程加载数据「GroupBuyActivityDiscountVO、SkuVO」完成", requestParameter.getUserId());
    }
}
